function [L] = knn2lap(idx,wet)
%KNN2LAP Returns the Laplacian matrix of the directed graph built by the k nearest neighbors
%   L = KNN2LAP(IDX) returns the sparse matrix L
%
%
%   Suqi Liu, 05.13.2014

[n,k] = size(idx);
ivec = repmat((1:n)',k,1);
nvec = reshape(idx,[],1);
evec = repmat((1:(n*k))',2,1);
if (nargin == 1)
    B = sparse(evec,[ivec;nvec],[ones(n*k,1);-ones(n*k,1)]);
else
    wvec = reshape(wet,[],1);
    qvec = sqrt(wvec);
    B = sparse(evec,[ivec;nvec],[qvec;-qvec]);
end
L = B'*B;
end